import axios from 'axios'
import qs from 'qs'
// import Router from '../router/router'
import {
  Message
} from 'iview'
import theBaseUrl from './baseUrl'
Message.config({
  top: 200,
  duration: 2
})
// 创建axios实例 application/x-www-data-urlencoded  application/json
let Axios = axios.create({
  timeout: 600000,
  responseType: "json",
  withCredentials: true, // 是否允许带cookie这些
  headers: {
    // "Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
    "Content-Type": "application/json;charset=utf-8"
    // 'Access-Control-Max-Age': '0',
  }
});

if (process.env.NODE_ENV == 'development') {
  Axios.defaults.baseURL = theBaseUrl; //192.168.1.239  120.27.21.149 192.168.3.244
} else if (process.env.NODE_ENV == 'debug') {
  Axios.defaults.baseURL = theBaseUrl;
} else if (process.env.NODE_ENV == 'production') {
  Axios.defaults.baseURL = theBaseUrl;
}
//内网
// if (process.env.NODE_ENV == 'development') {
//   Axios.defaults.baseURL = 'http://192.168.3.244:9999'; //192.168.1.239  120.27.21.149 192.168.3.244
// } else if (process.env.NODE_ENV == 'debug') {
//   Axios.defaults.baseURL = 'http://192.168.3.244:9999';
// } else if (process.env.NODE_ENV == 'production') {
//   Axios.defaults.baseURL = 'http://192.168.3.244:9999';
// }

Axios.defaults.headers.post['X-Requested-With'] = 'XMLHttpRequest';
Axios.defaults.headers.get['X-Requested-With'] = 'XMLHttpRequest';
// Axios.defaults.transformRequest = [function(data) {
//     //数据序列化
//     return qs.stringify(data);
// }];

Axios.interceptors.request.use(
  config => {
    if (localStorage.getItem('token')) {
      // console.log(localStorage.getItem('token'));
      config.headers['token'] = localStorage.getItem('token')
    }
    // console.log('this is axios config>>>', config)
    return config;
  },
  error => {
    //error的回调信息，可定义
    console.error(error);
    return Promise.reject(error);
  }
);

//返回状态判断(添加响应拦截器)
Axios.interceptors.response.use(
  response => {
    const res = response;
    // console.log(res.status);
    // console.log(res);
    if (res.data.ret != 1) {
      if (res.data.ret == -1) {
        if (res.data.errinfo.errcode == -1) {
          console.log(res.data.errinfo.errinfo.sqlMessage.indexOf('car_num'))
          if (res.data.errinfo.errinfo.sqlMessage.indexOf('car_num') >= 0) {
            Message.error('车牌号已存在')
          } else if (res.data.errinfo.errinfo.sqlMessage.indexOf('deviceid') >= 0) {
            Message.error('设备号已存在')
          }
        } else if (res.data.info.errmsg) {
          Message.error(res.data.info.errmsg)
        } else if (res.data.errinfo.errmsg) {
          Message.error(res.data.errinfo.errmsg)
        }
      }
      if (res.data.ret == -3) {
        Message.warning('身份验证过期，请重新登录')
        Router.push({ path: '/login' })
      }
      if (res.data.ret == -4) {
        if (res.data.info.errmsg) {
          Message.error(res.data.info.errmsg)
        }
        if (res.data.errinfo.errmsg) {
          Message.error(res.data.errinfo.errmsg)
        }
      };
      return Promise.reject(res);
    } else {
      // if (!res.data.info) {
      //     Message.warning('暂无数据')
      // }
      if (res.data.info) {
        if (res.data.info.token) {
          localStorage.setItem('token', res.data.info.token)
        }
      }
      // console.log(res.data)
      return Promise.resolve(res.data);
    }
  },
  error => {
    // console.log('err', error) //for debug
    // Message.error('网络连接错误')
    return Promise.reject(error)
  }
)

export default Axios
